package com.lzx.applib.listener;

import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.view.View;
import android.widget.EditText;

/**
 * Created by Lzx on 16/12/05 0005.
 */
public class SimpleDeleteTextWatcher extends SimpleTextWatcher {
    public static final String TAG = "SimpleDeleteTextWatcher";
    private final EditText editText;
    private final View deleteView;

    public SimpleDeleteTextWatcher(final EditText editText, final View deleteView) {
        this.editText = editText;
        this.deleteView = deleteView;

        deleteView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editText.setText("");
            }
        });
        editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    deleteView.setVisibility(editText.length()>0 ? View.VISIBLE : View.GONE);
                } else {
                    deleteView.setVisibility(View.GONE);
                }
            }
        });
        editText.setSingleLine(true);
    }

    @Override
    public void afterTextChanged(Editable s) {
        if (s.length() > 0) {
            deleteView.setVisibility(View.VISIBLE);
        } else {
            deleteView.setVisibility(View.GONE);
        }
    }
}
